// 简单的API测试脚本
const https = require('https');
const http = require('http');

// 测试基础API调用
function testBasicAPI() {
    console.log('🧪 测试基础API调用...');
    
    const postData = JSON.stringify({
        prompt: '你好，请介绍一下你自己'
    });
    
    const options = {
        hostname: 'localhost',
        port: 8080,
        path: '/api/test/deepseek',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData)
        }
    };
    
    const req = http.request(options, (res) => {
        console.log(`状态码: ${res.statusCode}`);
        console.log(`响应头: ${JSON.stringify(res.headers)}`);
        
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            console.log('响应内容:', data);
            try {
                const response = JSON.parse(data);
                console.log('解析后的响应:', response);
            } catch (e) {
                console.log('响应不是有效的JSON');
            }
        });
    });
    
    req.on('error', (e) => {
        console.error(`请求错误: ${e.message}`);
    });
    
    req.write(postData);
    req.end();
}

// 测试角色扮演API
function testRoleplayAPI() {
    console.log('\n🎭 测试角色扮演API...');
    
    const postData = JSON.stringify({
        character: '柯南',
        prompt: '你好，请用柯南的语气和我对话'
    });
    
    const options = {
        hostname: 'localhost',
        port: 8080,
        path: '/api/test/roleplay',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData)
        }
    };
    
    const req = http.request(options, (res) => {
        console.log(`状态码: ${res.statusCode}`);
        
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            console.log('响应内容:', data);
            try {
                const response = JSON.parse(data);
                console.log('解析后的响应:', response);
            } catch (e) {
                console.log('响应不是有效的JSON');
            }
        });
    });
    
    req.on('error', (e) => {
        console.error(`请求错误: ${e.message}`);
    });
    
    req.write(postData);
    req.end();
}

// 运行测试
console.log('🚀 开始API测试...\n');
testBasicAPI();
setTimeout(() => {
    testRoleplayAPI();
}, 2000);
